/**
 * Copyright 2008 Frank van Lingen Licensed under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package superpodder.controller.feedreader;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import superpodder.controller.FeedDownloader;

public  class FeedReader {
    
    //FIXME: logic of the subclasses for dealing with errors can be better.
    protected FeedDownloader feedDownloader = null;
    
    /*
     * Counts the number of format errors for a feed.
     * If to high it switches feed reader.
     */
    protected int formatErrors = 0;
    
    /*
     * Maximum number of format errors per feed.
     */
    protected int maxFormatErrors = 20;
    
    public FeedReader(FeedDownloader feedDownloader){
        this.feedDownloader = feedDownloader;
        System.out.println("FeedReader base");
    }

    public  String[] extractPodCasts(String feed, int amount)throws Exception{
        String[] result = new String[0];
        return result;
    }
   

    public boolean rightFormat(String downloadLoc) throws Exception {
        
       
        
        if ((downloadLoc == null)
                || !(downloadLoc.startsWith("http") && (downloadLoc
                        .matches(".*(mp3|mp4|mov|torrent)")))) {

            this.formatErrors += 1;
            return false;

        }
        
        /*
        if (!downloadLoc.endsWith("torrent")) {
            HttpURLConnection urlConnection = null;
            InputStream inputStreamReader = null;
            try {
                
                 //we might have gotten a url that points to nothing so we need
                 //to check if we can read data from that (we read 32 bytes). If
                 //we can not read it we try another reader.
                 
                final URL podCastUrl = new URL(downloadLoc);
                urlConnection = (HttpURLConnection) podCastUrl.openConnection();
                urlConnection.connect();
                inputStreamReader = urlConnection.getInputStream();
                inputStreamReader.read(new byte[32]);
                urlConnection.disconnect();
                inputStreamReader.close();
            } catch (Exception se) {
                this.feedDownloader.log("Problem downloading file: "+se.getMessage()+"\n\n");
                this.formatErrors += 1;
                // if there was an exception this might have left some
                // connections open. Make sure these are closed.
                try{
                    urlConnection.disconnect();
                } catch (Exception se1){
                    System.out.println("ignore exception");
                }
                try{
                    inputStreamReader.close();
                } catch (Exception se1){
                    System.out.println("ignore exception");
                }
                
                return false;
            }
         
        }
           */
       
        return true;
       
    }
    
    protected void reachedMaxErrors() throws Exception{
        if (this.formatErrors >= this.maxFormatErrors){
            String msg = "Too many format errors with this feedreader, trying to switch reader";
            this.feedDownloader.log(msg+"\n\n");
            throw new Exception(msg);
        }
    }
}
